<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <style>
        .aui-bar-nav {
            background: none;
            color: #FFF;
        }

        .aui-bar-nav .aui-iconfont {
            color: #FFF!important;
            font-weight: bold;
        }

        .trial-detail .cent-box .item button[disabled] {
            opacity: 0.6;
        }
    </style>
</head>

<body>
    <div class="trial-detail">
        <header id="header" data-color="light" class="aui-bar aui-bar-nav">
            <a class="aui-pull-left aui-btn" tapmode onclick="comeBackAgin()">
                <span class="aui-iconfont aui-icon-left"></span>
            </a>
            <div class="aui-title">任务详情</div>
        </header>
        <div class="aui-content">
            <div class="top-box">
                <div><img id="wordimg" src="../icon/logo.png"> </div>
                <h2>+<span id="taskmoney">0</span>元</h2>
            </div>
            <style>
                        .timer-box{
                          margin-bottom: 0px !important;
                        }
                        .cent-box{
                          margin-top: 20px !important;
                        }
            </style>
            <div class="timer-box" style="display:none">剩余时间<span>00:00</span></div>
            <div class="cent-box">
                <div class="bage">第一步</div>
                <div class="item">
                    <p>APP Store搜索关键词</p>
                    <h2 id="keyword">“<span>一元秒杀</span>”</h2>
                    <div class="row">
                        <p>APP Store搜索排名第<span id="paiming">1</span>位</p>
                        <button tapmode onclick="goDownload(this);" type="button">前去下载</button>
                    </div>
                </div>
            </div>
            <div class="cent-box">
                <div class="bage">第二步</div>
                <div class="item last">
                    <p>打开应用进入界面，试玩3分钟</p>
                    <div class="row">
                        <p>打开时请“允许”蜂窝移动网络</p>
                        <button type="button" id="appID" onclick="openApp(this);">打开APP</button>
                    </div>
                </div>
            </div>
            <div class="bott-btn"><button type="button" tapmode onclick="getTask();">领取奖励</button></div>
        </div>
        <!-- 蒙层弹框 -->
        <div class="mask-box" style="display:none">
            <div class="cont after" style="display:none">
                <h3>马上就可以领取奖金了呢 </br> 确定放弃该任务么？</h3>
                <p style="display:none">该任务的奖励发放与否，由广告 直接判定。请务必确保允许应用领不到奖励。
                </p>
                <div class="btn"><button type="button" tapmode onclick="closeWordBack();">确定</button></div>
                <div class="btn cancel"><button type="button" tapmode onclick="closeMask();">取消</button></div>
            </div>
            <div class="cont zhongjian" style="display:none">
                <p>该任务的奖励发放与否，由广告 直接判定。请务必确保允许应用领不到奖励。</p>
                <div class="btn"><button type="button" tapmode onclick="closeZhong();">确定</button></div>
            </div>
            <div class="cont last">
                <h3>任务已开始</h3>
                <p>请在桌面App Store</p>
                <div class="text">
                    <p><span>搜索</span><span id="maskword">“一元秒杀”</span></br>下载第<span id="maskpai">1</span>个应用 </p>
                </div>
                <div class="las-bottom"><img src="../image/122.png"></div>
                <div class="last-after"><img src="../image/13.png" tapmode onclick="closeMask();"></div>
            </div>
        </div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../script/md5.min.js"></script>
<script type="text/javascript">
    var isback = true;
    var isdownload = 1;
    var iosUrl = '';
    var copytext = '';
    var endTime = 0; //结束时间字符串
    var rid;
    apiready = function() {
        var date = new Date();
        var time = date.getTime(); //当前时间距1970年1月1日之间的毫秒数
        endTime = time + 1800000;
        initHeard();
        getPageInit();
    };

    function closeZhong() {
        $(".mask-box").hide();
        $(".mask-box .zhongjian").hide();
    };
    //初始化页面
    function getPageInit() {
        _ajax("index/taskInfo", {
            tid: api.pageParam.tid
        }, function(res, err) {
            if (res) {
                if (res.errCode != 0) {
                    $("#keyword span").html(res.data.keywords); //搜索关键词
                    $("#maskword").html("“" + res.data.keywords + "”"); //搜索关键词
                    $("#maskpai").html(res.data.step); //排名
                    $(".timer-box span").html(res.data.end_time + '分钟'); //剩余时间
                    $("#paiming").html(res.data.step); //排名
                    if (res.data.img != '' && res.data.img != null)
                        $("#wordimg").attr("src", imgurl + res.data.img); //任务图标
                    $("#taskmoney").html(res.data.task_money); //任务奖励金额
                    iosUrl = res.data.appid;
                    copytext = res.data.keywords;
                    console.log(api.pageParam.tid+'///////////');
                    _ajax("index/taskReceive", {
                        tid: api.pageParam.tid
                    }, function(res, err) {   
                        if (res) {
                            _msg(res.msg);
                            if (res.errCode == 1) {
                                isback = false;
                                console.log('抢任务成功');
                                // task_time_due();
                                // $api.setStorage('rid',res.data.rid);
                                console.log(JSON.stringify(res))
                              rid=res.data
                              console.log(JSON.stringify(rid))
                            } else if (res.errCode == -1) {
                                openWin('login');
                            }
                        } else {
                   
                            _msg("网络错误");
                        }
                    })
                } else {
            
                    _msg(res.msg);
                }
            } else {
                _msg("网络错误");
            }
        })
    };
    //去下载
    function goDownload() {
        usercopy(copytext);
        if (isdownload == 1)
            isdownload = 2;
        api.appInstalled({
            appBundle: iosUrl
        }, function(ret, err) {
            if (ret.installed) {
                _msg("应用已安装");
            } else {
                api.openApp({
                    iosUrl: 'itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/search?'
                });
            }
        });
    };
    //打开app
    function openApp() {
        if (isdownload == 2)
            isdownload = 3;
        if (api.systemType == "android") {
            _msg("抱歉~只有iphone才能使用此功能");
            return false;
        }
        //异步返回结果：
        api.appInstalled({
            appBundle: iosUrl
        }, function(ret, err) {
            if (ret.installed) {
                api.openApp({
                    iosUrl: iosUrl
                });
                  $('.timer-box').show();
                  task_time_due();
                _ajax("index/loading", {
                    tid: api.pageParam.tid,
                    rid: rid
                }, function(res, err) {
                })
            } else {
                //应用未安装
                _msg("应用未安装,请先安装");
            }
        });
    };
    //返回按钮
    function comeBackAgin() {
        console.log(12312);
        if (!isback) {
            $(".mask-box>div").hide();
            $(".mask-box .after").show();
            $(".mask-box").show();
        } else {
            comeBack();
        }
    };
    // 领取任务奖励
    function getTask() {
        console.log(isdownload);
        if (isdownload == 1 || isdownload == 2) {
            _msg("请先下载打开试玩！");
            return;
        } else if (isdownload == 4) {
            _msg('奖金已领取，不能重复领取！');
            return;
        }
        console.log(rid);
        _ajax("index/getMoney", {
            tid: api.pageParam.tid,
            rid:rid
        }, function(res, err) {
            if (res) {
                if (res.errCode == 1) {
                    _msg(res.msg);
                    isback = true;
                    isdownload = 4;
                    setTimeout(function() {
                        comeBack();
                    }, 800)
                } else {
                    $(".mask-box .zhongjian>p").html(res.msg);
                    $(".mask-box>div").hide();
                    $(".mask-box").show();
                    $(".mask-box .zhongjian").show();
                }
            } else {
                _msg("网络错误");
            }
        })
    };
    //取消操作
    function closeMask() {
        $(".mask-box").hide();
    };
    //  确认放弃任务
    function closeWordBack() {
        _ajax("index/giveup", {
            tid: api.pageParam.tid
        }, function(res, err) {
            if (res) {
                _msg(res.msg);
            }
            comeBack();
        })
    }

    // 倒计时函数
    function task_time_due() {
        var date = new Date();
        var time = date.getTime(); //当前时间距1970年1月1日之间的毫秒数
        $('.timer-box').each(function(i) {
            var lag = (endTime - time); //当前时间和结束时间之间的秒数
            // console.log(lag);
            if (lag > 0) {
                var second = Math.floor(lag / 1000 % 60);
                var minite = Math.floor(lag / 1000 / 60 % 60);
                var hour = Math.floor(lag / 1000 / 60 / 60 % 24);
                var day = Math.floor(lag / 1000 / 60 / 60 / 24);
                second = second.toString().length == 2 ? second : second;
                minite = minite.toString().length == 2 ? minite : minite;
                hour = hour.toString().length == 2 ? hour : hour;
                day = day.toString().length == 2 ? day : day;
            } else {
                var second = '0';
                var minite = '0';
                var hour = '0';
                var day = '0';
            }
            $(".timer-box span").html(minite + "分" + second + "秒")
        });
        setTimeout(function() {
            task_time_due()
        }, 1000);

        };

        // 时间搓处理函数
        function task_time_cuo() {
            var date = new Date();
            var time = date.getTime(); //当前时间距1970年1月1日之间的毫秒数
            $('.timer-box').each(function(i) {
                var lag = (endTime - time); //当前时间和结束时间之间的秒数
                // console.log(lag);
                if (lag > 0) {
                    var second = Math.floor(lag / 1000 % 60);
                    var minite = Math.floor(lag / 1000 / 60 % 60);
                    var hour = Math.floor(lag / 1000 / 60 / 60 % 24);
                    var day = Math.floor(lag / 1000 / 60 / 60 / 24);
                    second = second.toString().length == 2 ? second : second;
                    minite = minite.toString().length == 2 ? minite : minite;
                    hour = hour.toString().length == 2 ? hour : hour;
                    day = day.toString().length == 2 ? day : day;
                } else {
                    var second = '0';
                    var minite = '0';
                    var hour = '0';
                    var day = '0';
                }
                $(".timer-box span").html('约'+minite + "分")
            });

    };
</script>

</html>
